package com.wenwu.controller.movable;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsAudioVisual;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.service.CommonService;
import com.wenwu.service.MoveableAudioVisualService;


/**
 * 可移动文物声像影片表
 * @author Yaphet
 *
 */

@Controller(value = "MoveableAudioVisualController")
@RequestMapping(value = "/mgr/maudiovisual")
public class MoveableAudioVisualController {
	Logger log = Logger.getLogger(this.getClass());
	private JsonGenerator jsonGenerator;
	
	@Autowired
	private CommonService commonService;
	@Autowired
	private MoveableAudioVisualService moveableAudioVisualService;
	
	/**
	 * 初始化界面
	 * @return
	 */
	@RequestMapping(value = "/init")
	public String initUserPage() {
		log.info("进入基本信息管理页面...");
		return "maudiovisual/init";
	}
	
	
	/**
	 * 可移动文物登记表表单查询
	 * @return
	 */
	@RequestMapping(value = "/showList")
	public void showList(RelicsAudioVisual relicsAudioVisual,Model model, HttpServletRequest request,
			HttpServletResponse response) {
		log.info("查询可移动文物登记表表单..." );
		PaginationVO pageVo = PageUtils.getPaginationInfo(request);
		try {
			if(relicsAudioVisual!=null && relicsAudioVisual.getmRelicsNo() !=null){
				relicsAudioVisual.setmRelicsNo(relicsAudioVisual.getmRelicsNo().trim());
			}
			if(relicsAudioVisual!=null && relicsAudioVisual.getRelicsName() !=null){
				relicsAudioVisual.setRelicsName(relicsAudioVisual.getRelicsName().trim());
			}
			
			pageVo = moveableAudioVisualService.selectByCondition(relicsAudioVisual, pageVo);
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(pageVo);// 将PaginationVO对象以json格式写回页面
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			model.addAttribute("errorMsg", "系统异常，请稍后重试!");
		}
	}
	
	/**
	 * 进入新增可移动文物声像影片表单页面
	 * @return
	 */
	@RequestMapping(value = "/audiovisualform")
	public String mcuform(Model model,
			@RequestParam(required = false, value = "mRelicsId") Integer mRelicsId,
			@RequestParam(required = false, value = "relicsName") String relicsName,
			@RequestParam(required=false,value = "audioId") Integer audioId) {
		log.info("进入可移动文物声像影片页面...");
		try {
			if(audioId!=null){
				List<RelicsAttachment> attachments;
				attachments = commonService.selectAttachmentListByKey(
						audioId, AttachmentFrom.RELICS_AUDIO_VISUAL_01,
						AttachmentFrom.RELICS_AUDIO_VISUAL_02);
				model.addAttribute("attachments", attachments);
			}else if (mRelicsId != null) {// 查询对应的附件集合
				List<RelicsAttachment> attachments;

				attachments = commonService.selectAttachmentListByKey(
						mRelicsId, "relics_audio_visual", "audio_id",
						"m_relics_id", AttachmentFrom.RELICS_AUDIO_VISUAL_01,
						AttachmentFrom.RELICS_AUDIO_VISUAL_02);
				model.addAttribute("attachments", attachments);
				model.addAttribute("relicsName",
						URLDecoder.decode(relicsName, "UTF-8"));

			}
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return "maudiovisual/audiovisualform";
	}
	
	/**
	 * 进入新增可移动文物声像影片添加/修改页面
	 * @return
	 * @throws Exception 
	 * @throws  
	 */
	@RequestMapping(value = "/showAudiovisualform")
	public void updateMcuform(HttpServletResponse response,
			@RequestParam(required=false,value = "mRelicsId") Integer mRelicsId,
			@RequestParam(required=false,value = "audioId") Integer audioId) {
		log.info("进入可移动文物声像影片页面...");
		try{
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			if(audioId !=null){
				jsonGenerator.writeObject(moveableAudioVisualService.selectAudioVisualByKey(audioId));
			}else if (mRelicsId!=null){
				jsonGenerator.writeObject(moveableAudioVisualService.selectAudioVisualByMWenwuKey(mRelicsId));
			}
			
		}catch(Exception e){
			log.info(e.getMessage(),e);
		}
	}
	
	/**
	 * 进入新增可移动文物声像影片
	 * @return
	 */
	@RequestMapping(value = "/saveAudiovisualform")
	public void savemcuform(RelicsAudioVisual relicsAudioVisual,HttpServletResponse response){
		log.info("进入可移动文物声像影片页面...:"+relicsAudioVisual);
		try {
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(moveableAudioVisualService.addRelicsAudioVisual(relicsAudioVisual));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
		}
	}
	
	@RequestMapping(value = "/deleteAudiovisualSelft")
	public void deleteMRformSelft(HttpServletResponse response,
			@RequestParam(value = "keys") String keys) {
		log.info("进入基本信息表单页面...");
		try{
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(moveableAudioVisualService.deleteBykeys(keys));
		}catch(Exception e){
			log.info(e.getMessage(),e);
		}
	}
}
